package org.caw.hbase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseTablePool {

	private HBaseConfigurationBean configuration;

	private HTablePool tablePool;

	public void init() {
		
		tablePool = new HTablePool(configuration.getConf(), 20);
	}

	public void destroy() {
		HConnectionManager.deleteAllConnections(true);
	}

	public HTableInterface getTable(String tableName) {
		return tablePool.getTable(tableName);
	}

	public void putTable(HTableInterface tableInterface) {
		tablePool.putTable(tableInterface);
	}


	public List<String> getTableFamilies(String tableName) throws IOException {
		HTableInterface table = tablePool.getTable(tableName);
		List<String> columns = new ArrayList<String>();
		for (byte[] family : table.getTableDescriptor().getFamiliesKeys()) {
			columns.add(Bytes.toString(family));
		}
		return columns;
	}

	public HBaseConfigurationBean getConfiguration() {
		return configuration;
	}

	public void setConfiguration(HBaseConfigurationBean configuration) {
		this.configuration = configuration;
	}
}
